<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item label="单据编号" prop="number">
        <el-input v-model="queryParams.number" placeholder="请输入单据编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="单据日期" prop="createTime">
        <el-date-picker v-model="createTimeRange" type="datetimerange" range-separator="至" start-placeholder="开始日期"
          end-placeholder="结束日期" />
      </el-form-item>
      <el-form-item label="商品信息" prop="masterialList">
        <el-input v-model="queryParams.materialParam" placeholder="输入条码、名称、规格..." clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="操作员" prop="creator">
        <el-input v-model="queryParams.creator" placeholder="请输入操作员" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="供应商">
        <el-select v-model="queryParams.supplierId" placeholder="请选择">
          <el-option v-for="item in supplierList" :key="item.supplier" :label="item.supplier" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="单据备注" prop="remark">
        <el-input v-model="queryParams.remark" placeholder="请输入关联订单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="单据状态" prop="status">
        <el-select v-model="queryParams.status" class="select1" placeholder="选择单据状态" size="small">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="plus" @click="handleAdd">新增</el-button>
      </el-col>
      <el-dropdown>
        <el-button type="primary" style="margin-left:10px;">
          批量操作<i class="el-icon-arrow-down el-icon--right" />
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item icon="el-icon-delete" @click.native="batchDel">删除</el-dropdown-item>
          <el-dropdown-item icon="el-icon-success" @click.native="batchSetStatus('1')">审核</el-dropdown-item>
          <el-dropdown-item icon="el-icon-error" @click.native="batchSetStatus('0')">反审核</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="headList" @selection-change="handleSelectionChange1">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单据编号" align="center" prop="number" width="155" />
      <el-table-column label="单据日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ timestampToTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作员" align="center" prop="creator" />
      <el-table-column label="数量" align="center" prop="num" />
      <el-table-column label="商品信息" align="center" prop="materialsList" />
      <el-table-column label="金额合计" align="center" prop="allPrice" />
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" type="danger">未审核</el-tag>
          <el-tag v-if="scope.row.status == 1" type="success">已审核</el-tag>
          <el-tag v-if="scope.row.status == 2" type="danger">完成采购</el-tag>
          <el-tag v-if="scope.row.status == 3" type="danger">部分采购</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermi="['depot:head:find']" size="mini" type="text" icon="el-icon-delete"
            @click="findDepotByHeadId(scope.row.id)">查看</el-button>
          <el-button v-hasPermi="['depot:head:edit']" size="mini" type="text" icon="el-icon-edit"
            @click="handleUpdate(scope.row.id)">修改</el-button>
          <el-button v-hasPermi="['depot:head:remove']" size="mini" type="text" icon="el-icon-delete"
            @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" :visible.sync="findDepot" width="1062px" append-to-body>
      <el-form ref="form" label-width="90px">
        <el-descriptions class="margin-top" :column="4">
          <el-descriptions-item label="单据日期">{{ findDepotTime }}</el-descriptions-item>
          <el-descriptions-item label="单据编号">{{ depot.number }}</el-descriptions-item>
          <el-descriptions-item label="关联单据">{{ depot.linkNumber }}</el-descriptions-item>
        </el-descriptions>
        <el-table ref="multipleTable" border :data="findDepotList" tooltip-effect="dark" style="width: 100%"
          @selection-change="handleSelectionChange">
          <el-table-column prop="depotName" label="仓库名称" width="120" />
          <el-table-column prop="barCode" label="条码" width="80" />
          <el-table-column prop="name" label="名称" width="80" />
          <el-table-column prop="standard" label="规格" width="80" />
          <el-table-column prop="model" label="型号" width="80" />
          <el-table-column prop="color" label="颜色" width="80" />
          <el-table-column prop="materialOther" label="扩展信息" width="140" />
          <el-table-column prop="stock" label="库存" width="60" />
          <el-table-column prop="unit" label="单位" width="60" />
          <el-table-column prop="operNumber" label="数量" width="60" />
          <el-table-column prop="unitPrice" label="单价" width="60" />
          <el-table-column prop="allPrice" label="金额" width="60" />
          <el-table-column prop="remark" label="备注" width="60" />
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-pagination style="float: right" :current-page="queryParams.currentPage" :page-sizes="[5, 10, 15, 20]"
      :page-size="queryParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"
      @size-change="getListPageSize" @current-change="getListPageCurrent" />

    <!-- 添加或修改单据主对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <b>单据日期:</b>
        <el-date-picker v-model="info.createTime" type="datetime" placeholder="选择单据日期" />
        <b>单据编号:</b><el-autocomplete v-model="info.number" disabled />
        <el-divider content-position="center" />
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddJshDepotItem">插入行</el-button>
          </el-col>
          <el-input v-model="mBarCode" placeholder="输入条码并回车" @keyup.enter.native="inputmBarCode(14)" />
          <el-select v-model="d" placeholder="仓库操作" width="50" @change="getValue" readonly>
            <el-option v-for="item in ddd" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>

          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteJshDepotItem">删除</el-button>
          </el-col>
        </el-row>
        <el-dialog title="批量设置仓库" :visible.sync="plszck" width="600px" :append-to-body="true">
          *仓库名称：
          <el-select v-model="depotId" placeholder="请选择">
            <el-option v-for="item in depotList" :key="item.name" :label="item.name" :value="item.id" />
          </el-select>
          <div slot="footer" class="dialog-footer">
            <el-button @click="cance">取 消</el-button>
            <el-button type="primary" @click="defaultUpdate()">确 定</el-button>
          </div>
        </el-dialog>
        <el-dialog title="仓库信息" :before-close="handleClose" :visible.sync="dialogFormVisible" width="1000px"
          :append-to-body="true">
          <el-form :model="depotForm" size="mini" :rules="rules" ref="form">
            <el-form-item label="仓库名称:" prop="name" required label-width="90px">
              <el-input v-model="depotForm.name" style="width: 400px" placeholder="请输入仓库名称"></el-input>
            </el-form-item>
            <el-form-item label="仓库地址:" label-width="90px">
              <el-input v-model="depotForm.address" style="width: 400px" placeholder="请输入仓库地址"></el-input>
            </el-form-item>
            <el-form-item label="仓储费:" label-width="90px">
              <el-input v-model="depotForm.warehousing" style="width: 400px" placeholder="请输入仓储费">
                <template slot="append">元/天/KG</template>
              </el-input>
            </el-form-item>
            <el-form-item label="搬运费:" label-width="90px">
              <el-input v-model="depotForm.truckage" style="width: 400px" placeholder="请输入搬运费">
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
            <el-form-item label="负责人:" label-width="90px">
              <el-select v-model="depotForm.principal" placeholder="请选择负责人" style="width:400px;">
                <el-option v-for="item in option" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="排序:" label-width="90px">
              <el-input v-model="depotForm.sort" style="width: 400px" placeholder="请输入排序"></el-input>
            </el-form-item>
            <el-form-item label="备注:" label-width="90px">
              <el-input v-model="depotForm.remark" type="textarea" style="width: 400px" placeholder="请输入备注"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="cance">取 消</el-button>
            <el-button type="primary" @click="submitInOutItem('form')">确 定</el-button>
          </div>
        </el-dialog>
        <el-table ref="jshDepotItem" :data="jshDepotItemList" :row-class-name="rowJshDepotItemIndex"
          @selection-change="handleJshDepotItemSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="商品类别" align="center"  width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.materialType" readonly>
              </el-input>
            </template>
          </el-table-column>
          <el-table-column label="仓库名称" align="center" prop="depotId" width="150">
            <!-- <template slot-scope="scope">
              <el-select v-model="scope.row.depotId" placeholder="请选择仓库">
                <el-option v-for="item in depotList" :key="scope.row.depotId" :label="item.name" :value="item.id" />
              </el-select>
            </template> -->
          </el-table-column>
          <el-table-column label="条码" prop="mBarCode " width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.mbarCode"
                @keyup.enter.native="mBarCodeClick(scope.row.mbarCode, scope.$index, scope.row.depotId)">
                <i class="el-icon-edit el-input__icon" slot="suffix" @click="handleIconClick">
                </i>
              </el-input>
            </template>
          </el-table-column>
          <el-dialog :title="title" :visible.sync="xzsp" width="1090px" append-to-body>
            <el-form ref="formw" :model="materialQuery" size="small" :inline="true">
              <el-form-item label="商品名称">
                <el-input v-model="materialQuery.q" placeholder="" style="width: 240px" />
              </el-form-item>
              <el-form-item label="类别">
                <el-select v-model="materialQuery.categoryId" placeholder="" clearable style="width: 240px">
                  <el-option v-for="dict in categoryList" :key="dict.name" :label="dict.name" :value="dict.id" />
                </el-select>
              </el-form-item>
              <el-form-item label="批号">
                <el-select v-model="materialQuery.enableBatchNumber" placeholder="" clearable style="width: 240px">
                  <el-option v-for="dict in BatchN" :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
              </el-form-item>
              <el-form-item label="序列号">
                <el-select v-model="materialQuery.enableSerialNumber" placeholder="" clearable style="width: 240px">
                  <el-option v-for="dict in BatchN" :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
              </el-form-item>
              <el-form-item label="仓库名称">
                <el-select v-model="materialQuery.depotId" placeholder="请选择">
                  <el-option v-for="item in depotList" :key="item.name" :label="item.name" :value="item.id" />
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleIconClick">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini">重置</el-button>
                <el-button icon="el-icon-refresh" size="mini">新增</el-button>
              </el-form-item>
            </el-form>
            <div>
              <el-table ref="multipleTable" v-loading="loading" :data="tableData" style="width: 100%"
                tooltip-effect="dark" border @selection-change="handleSelectionChangeItem">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column prop="mbarCode" label="条码" width="60" />
                <el-table-column prop="name" label="名称" show-overflow-tooltip />
                <el-table-column prop="model" label="型号" width="60" />
                <el-table-column prop="standard" label="规格" width="60" />
                <el-table-column prop="color" label="颜色" width="60" />
                <el-table-column prop="unit" label="单位" show-overflow-tooltip />
                <el-table-column prop="sku" label="多属性" show-overflow-tooltip />
                <el-table-column prop="stock" label="库存" show-overflow-tooltip />
                <el-table-column prop="id" show-overflow-tooltip v-if="false" />
              </el-table>

              <el-pagination style="float: right" :current-page="this.materialQuery.page"
                :page-sizes="[5, 10, 20, 30, 40]" :page-size="this.materialQuery.rows"
                layout="total, sizes, prev, pager, next, jumper" :total="materialTotal"
                @size-change="getMaterialListPageSize" @current-change="getMaterialListPageCurrent" />
            </div>
            <br /><br />
            <div slot="footer" class="dialog-footer">
              <el-button @click="materialCancel">取 消</el-button>
              <el-button @click="materialCancel">确定</el-button>
            </div>
          </el-dialog>
          <el-table-column label="名称" prop="name" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.name" readonly />
            </template>
          </el-table-column>
          <el-table-column label="规格" prop="standard" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.standard" readonly />
            </template>
          </el-table-column>
          <el-table-column label="型号" prop="model" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.model" readonly />
            </template>
          </el-table-column>
          <el-table-column label="颜色" prop="color" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.color" readonly />
            </template>
          </el-table-column>
          <el-table-column label="拓展信息" prop="mfrs" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.mfrs" readonly />
            </template>
          </el-table-column>

          <el-table-column label="库存" prop="stock" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.stock" readonly />
            </template>
          </el-table-column>
          <el-table-column label="单位" prop="unit" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.unit" readonly />
            </template>
          </el-table-column>
          <el-table-column label="序列号" prop="enableSerialNumber" width="150">
            <template slot-scope="scope">
              <el-input placeholder="序列号" v-model="scope.row.enableSerialNumber" suffix-icon="el-icon-search"
                readonly></el-input>
            </template>
          </el-table-column>
          <el-table-column label="多属性" prop="sku" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.sku" readonly />
            </template>
          </el-table-column>
          <el-table-column label="数量" prop="operNumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.operNumber" placeholder="请输入数量" @input="inputChange(scope.$index)" />
            </template>
          </el-table-column>
          <el-table-column label="单价" prop="unitPrice" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.unitPrice" placeholder="请输入单价" @input="inputChange(scope.$index)" />
            </template>
          </el-table-column>
          <el-table-column label="金额" prop="billPrice" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.billPrice" placeholder="请输入金额" />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>

        <b>备注:</b><el-input type="text" v-model="info.remark" />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button type="primary" @click="submitFormStatus">保存并审核</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import supplierApi from '@/api/supplier'
import depot from '@/api/depot/depot'
import depotApi from '@/api/depot/depot'
import { getDepotHeadAndDetail, updateDepotHeadAndDetail, addDepotHeadAndDetail, getMaterialListByCode, getcategoryList, getDepotMaterialList, findDepot, buildNumber, deleteBatch, upStatus, listUser, getDepotNameList, listHead } from '@/api/depot/otherIn'
import { runInThisContext } from 'vm'

export default {
  name: 'Head',
  data() {
    return {
      depotForm: {},
      //商品弹出层
      xzsp: false,
      option: [{
        value: this.$store.getters.id,
        label: this.$store.getters.name
      }],
      materialQuery: {
        depotId: this.depotId,
        page: 1,
        rows: 5,
      },
      options: [
        {
          value: '1',
          label: '已审核'
        }, {
          value: '0',
          label: '未审核'

        }
      ],
      findDepot: false,
      info: {
        number: '',
        status: 0,
        type: '其他',
        subType: '拆卸单',
        jshDepotItemList: []
      },
      d: '',
      rows: [],
      BatchN: [{
        value: 1,
        label: '是'
      },
      {
        value: 0,
        label: '否'
      }],
      ddd: [{
        value: '0',
        label: '批量设置'
      }, {
        value: '1',
        label: '新增仓库'
      }],
      dialogFormVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      mBarCode: null,
      // 子表选中数据
      checkedJshDepotItem: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      materialTotal: 0,
      // 单据主表格数据
      headList: [],
      // 单据子表格数据
      jshDepotItemList: [],
      createTimeRange: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 删除标记，0未删除，1删除时间范围
      // 查询参数
      depot: {
        number: '',
        linkNumber: '',
        supplier: ''
      },
      categoryList: [],
      tableData: [],
      queryParams: {
        currentPage: 1,
        pageSize: 10,
        number: null,
        // beginTime:null,
        // endTime:null,
        organId: null,
        creator: null,
        status: null,
        linkNumber: null,
        remark: null,
        supplierId: '',
        materialParam: null,
        beginTime: null,
        endTime: null
      },
      type: 0,
      findDepotTime: '',
      supplierList: [],
      findDepotList: [
      ],
      listUser: [],
      // 表单参数
      form: {},
      plszck: false,
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    getcategoryList().then(res => {
      console.log(res.data)
      this.categoryList = res.data;
    });
    this.getList()
      supplierApi.supplierListApi('供应商').then(res => {
        this.supplierList = res.data
      })
    // listUser().then(res=>{
    //   this.listUser=res.data;
    // })
  },

  methods: {
    mBarCodeClick(code, index, depotId) {
      if (code != null && code.length == 4) {
        getMaterialListByCode(code, depotId).then(res => {
          debugger
          this.jshDepotItemList[index] = res.data;
          this.handleAddJshDepotItem();
          this.jshDepotItemList.splice(this.jshDepotItemList.length - 1, 1)
          console.log(res.data)
          console.log(index);
        })
      } else {
        this.handleIconClick();
      }
    },
    inputmBarCode(depotId) {
      if (this.mBarCode != null && this.mBarCode.length == 4) {
        getMaterialListByCode(this.mBarCode, depotId).then(res => {
          console.log(res.data.depotId)
          if (this.jshDepotItemList.length != 0) {
            this.jshDepotItemList.push(res.data)
          } else
            this.jshDepotItemList[0] = res.data;
          this.handleAddJshDepotItem();
          this.jshDepotItemList.splice(this.jshDepotItemList.length - 1, 1)
        })
      }
    },
    inputChange(index) {
      debugger
      this.jshDepotItemList[index].billPrice = (this.jshDepotItemList[index].operNumber * this.jshDepotItemList[index].unitPrice)
    },
    // 分页（首页）
    getListPageCurrent(val) {
      this.queryParams.currentPage = val;
      this.getList();
    },
    getListPageSize(val) {
      this.queryParams.pageSize = val;
      this.getList();
    },
    // 分页（商品）
    getMaterialListPageSize(val) {
      this.materialQuery.rows = val;
      this.handleIconClick();
    },
    getMaterialListPageCurrent(val) {
      this.materialQuery.page = val;
      this.handleIconClick();
    },
    // 条码按钮点击事件
    handleIconClick() {
      getDepotMaterialList(this.materialQuery).then(res => {
        this.xzsp = true;
        console.log(res.rows)
        this.tableData = res.rows;
        this.materialTotal = res.total;
      })

    },
    handleClose() {
      this.depotForm = {}
      this.dialogFormVisible = false
    },
    defaultUpdate() {
      if (this.depotId != null && this.depotId != '') {
        depotApi.isDefaultUpdate(this.depotId)
          .then(res => {
            this.$message({
              type: 'success',
              message: '修改成功'
            })
            for (var i = 0; i < this.jshDepotItemList.length; i++) {
              this.jshDepotItemList[i].depotId = this.depotId
            }
            this.plszck = false;
          })
      }
    },
    submitInOutItem(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          depotApi.addDepot(this.depotForm)
          this.$message({
            type: 'success',
            message: '操作成功'
          })
          this.dialogFormVisible = false
          this.depotForm = {}
        }
      })
    },
    getValue() {
      if (this.d == '1') {
        this.dialogFormVisible = true;
      } else if (this.d == '0') {
        this.plszck = true;
      }
    },
    // 查看
    findDepotByHeadId(id) {
      this.findDepot = true
      findDepot(id, 1).then(res => {
        this.findDepotList = res.rows
        this.depot.number = res.rows[0].number
        this.depot.linkNumber = res.rows[0].linkNumber
        this.depot.supplier = res.rows[0].supplier
        this.depot.createTime = res.rows[0].createTime
        this.findDepotTime = this.timestampToTime(res.rows[0].createTime)
      })
    },
    /** 查询单据主列表 */
    getList() {
      this.loading = true
      if (this.createTimeRange != null && this.createTimeRange != '') {
        this.queryParams.beginTime = this.timestampToTime(this.createTimeRange[0])
        this.queryParams.endTime = this.timestampToTime(this.createTimeRange[1])
      }
      listHead(this.queryParams).then(response => {
        this.headList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    timestampToTime(timestamp) {
      var date = new Date(timestamp)// 时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var Y = date.getFullYear() + '-'
      var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
      var D = date.getDate() + ' '
      var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':'
      var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':'
      var s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds())
      return Y + M + D + h + m + s
    },
    // 取消按钮
    cance() {
      this.dialogFormVisible = false,
        this.plszck = false;
    },
    cancel() {
      this.open = false
      this.findDepot = false
      this.dialogFormVisible = false
      this.reset()
      this.xzsp = false
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        subType: null,
        defaultNumber: null,
        number: null,
        createTime: null,
        operTime: null,
        organId: null,
        creator: null,
        accountId: null,
        changeAmount: null,
        backAmount: null,
        totalPrice: null,
        payType: null,
        billType: null,
        remark: null,
        fileName: null,
        salesMan: null,
        accountIdList: null,
        accountMoneyList: null,
        discount: null,
        discountMoney: null,
        discountLastMoney: null,
        otherMoney: null,
        deposit: null,
        status: null,
        purchaseStatus: null,
        linkNumber: null,
        tenantId: null,
        deleteFlag: null
      }
      this.jshDepotItemList = []
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.currentPage = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.createTimeRange = []
      queryParams.number = null,
        queryParams.organId = null,
        queryParams.creator = null,
        queryParams.status = null,
        queryParams.linkNumber = null,
        queryParams.remark = null,
        queryParams.depotId = null,
        queryParams.materialParam = null,
        queryParams.beginTime = null,
        queryParams.endTime = null,
        this.handleQuery()
    },
    batchSetStatus(status) {
      if (this.ids == null||this.ids=='') {
        this.$message.warning('请至少选择一条数据操作！')
      } else {
        this.$confirm('是否操作选中数据?', '确认操作', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const ids = []
        console.log(this.ids);
          // 遍历勾选数据取自己需要的id等这里是和后端需要的参数对应
          for (let i = 0; i < this.ids.length; i++) {
            ids.push(this.ids[i])// 把取出的对象push到数组里
          }
          upStatus(ids, status)
            .then(res => {
              this.$message({
                type: 'success',
                message: '修改成功!'
              })
              this.getList()
            })
        })
      }
    },
    batchDel() {
      if (this.ids == null||this.ids == ''||this.ids.length==0) {
        this.$message.warning('请至少选择一条数据进行删除！')
      } else {
        this.$confirm('是否删除选中数据', '确认删除', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
          // 声明空数组,这里的数组是后端需要的数据
          const ids = []
          // 遍历勾选数据取自己需要的id等这里是和后端需要的参数对应
          for (let i = 0; i < this.ids.length; i++) {
            ids.push(this.ids[i])// 把取出的对象push到数组里
          }
          console.log(this.ids);
          deleteBatch(ids).then(res => { // 接口
            this.getList()// 删完成功后初始化查询列表
          })
        })
      }
    },
    // 多选框选中数据
    handleSelectionChange1(selection) {
      this.ids = selection.map(item => item.id)
      console.log(this.ids);
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    materialCancel() {
      console.log(this.jshDepotItemList);
      this.xzsp = false
    },
    handleSelectionChangeItem(selection) {
      console.log(selection.depotId);
      this.jshDepotItemList = selection;
      for(var i=0;i<this.jshDepotItemList.length;i++){
        this.jshDepotItemList[i].operNumber=1
        this.jshDepotItemList[i].materialType='组合件'
        this.jshDepotItemList[i].billPrice = (this.jshDepotItemList[i].operNumber * this.jshDepotItemList[i].unitPrice)
      }

    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.info.jshDepotItemList=[];
      this.info.remark=""
      buildNumber(this.queryParams.type, this.queryParams.subType).then(res => {
        this.info.number = res.defaultNumber
      })
      this.info.createTime=new Date()
      this.type = 0;
      handleAddJshDepotItem()
    },
    /** 修改按钮操作 */
    handleUpdate(id) {
      getDepotHeadAndDetail(id).then(res => {
        this.info = res.info;
        this.jshDepotItemList = res.jshDepotItemList;
        for(var i=0;i<this.jshDepotItemList.length;i++){
        this.jshDepotItemList[i].materialType='组合件'
             }
        this.open = true;
        this.type = 1;
      })
    },
    /** 新增 */
    submitForm() {
      var aa=true;
      for(var i=0;i<this.jshDepotItemList.length;i++){
        if(this.jshDepotItemList[i].depotId==null||this.jshDepotItemList[i].depotId==''){
          aa=false;
        }
      }
      if(aa){
      this.info.jshDepotItemList = this.jshDepotItemList
      console.log(this.jshDepotItemList)
      console.log(this.info.jshDepotItemList)
      if (this.type == 0) {
        addDepotHeadAndDetail(this.info).then(res => {
          if (res.code == '200') {
            this.cancel(),
            this.getList();
            location.reload();
          }
        })
      } else {
        updateDepotHeadAndDetail(this.info).then(res => {
          this.cancel(),
          this.getList();
        })
      }
    }else{
      alert('请选择仓库')
    }
    },
    submitFormStatus() {
      this.info.status = 1;
      this.submitForm()
    },
    /** 删除按钮操作 */
    handleDelete(id) {
      this.$confirm('是否确认删除单据主编号为' + id + '的数据项？').then(function () {
        return deleteBatch(id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => { })
    },
    /** 单据子序号 */
    rowJshDepotItemIndex({ row, rowIndex }) {
      row.index = rowIndex + 1
    },
    /** 单据子添加按钮操作 */
    handleAddJshDepotItem() {
      const obj = {}
      obj.materialId = ''
      obj.materialExtendId = ''
      obj.materialUnit = ''
      obj.sku = ''
      obj.operNumber = 1
      obj.basicNumber = ''
      obj.unitPrice = ''
      obj.purchaseUnitPrice = ''
      obj.taxUnitPrice = ''
      obj.allPrice = ''
      obj.remark = ''
      obj.depotId = ''
      obj.anotherDepotId = ''
      obj.taxRate = ''
      obj.taxMoney = ''
      obj.taxLastMoney = ''
      obj.materialType = '组合件'
      obj.snList = ''
      obj.batchNumber = ''
      obj.expirationDate = ''
      obj.linkId = ''
      obj.tenantId = ''
      obj.mbarCode = ''
      this.jshDepotItemList.push(obj)
    },
    /** 单据子删除按钮操作 */
    handleDeleteJshDepotItem() {
      if (this.checkedJshDepotItem.length == 0) {
        this.$modal.msgError('请先选择要删除的单据子数据')
      } else {
        const jshDepotItemList = this.jshDepotItemList
        const checkedJshDepotItem = this.checkedJshDepotItem
        this.jshDepotItemList = jshDepotItemList.filter(function (item) {
          return checkedJshDepotItem.indexOf(item.index) == -1
        })
      }
    },
    /** 复选框选中数据 */
    handleJshDepotItemSelectionChange(selection) {
      this.checkedJshDepotItem = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/head/export', {
        ...this.queryParams
      }, `head_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
